﻿/*******************************************************************************
 * Copyright © Chris Watson 2013                                               *
 *                                                                             *
 * This document is a part of the source code and related artefacts for Chris  *
 * Watson's Desktop Wallpaper Guard, a software application which will detect  *
 * when another process removes the wallpaper from a Windows desktop and       *
 * restore an image of the user's choice.                                      *
 *                                                                             *
 * Author:                                                                     *
 *   Chris Watson       [mailto:chris@watson.me.uk]                            *
 * Date:                                                                       *
 *   17 August 2013                                                            *
 ******************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Watson.BleaknessEliminator.Util
{
    /// <summary>
    /// Provides information about the assembly.
    /// </summary>
    /// <remarks>
    /// Clients can easily access the class' properties through the static
    /// Default property.
    /// </remarks>
    public class AssemblyInfo
    {
        #region Private Static Read-only Fields

        /// <summary>
        /// Assembly object representing current Assembly.
        /// </summary>
        private static readonly System.Reflection.Assembly assembly =
            System.Reflection.Assembly.GetExecutingAssembly();

        /// <summary>
        /// The default instance of the AssemblyInfo class.
        /// </summary>
        private static readonly AssemblyInfo defaultInstance =
            new AssemblyInfo();

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the assembly's copyright information.
        /// </summary>
        public string Copyright
        {
            get
            {
                // get an array of AssemblyCopyrightAttribute attributes for 
                // the assembly
                object[] copyrightAttributes = 
                    assembly.GetCustomAttributes(
                        typeof(System.Reflection.AssemblyCopyrightAttribute),
                        false);

                if (copyrightAttributes.Length > 0)
                {
                    // if we've got any, return the first one
                    return ((System.Reflection.AssemblyCopyrightAttribute)copyrightAttributes[0])
                        .Copyright;
                }
                else
                {
                    // no copyright information
                    return "";
                }
            }
        }
        

        /// <summary>
        /// Gets an array of strings describing the libraries that this
        /// assembly depends upon.
        /// </summary>
        public string[] Dependencies
        {
            get
            {
                string[] depends = new string[0] { };
                return depends;
            }
        }


        /// <summary>
        /// Gets the display name of the assembly.
        /// </summary>
        public string DisplayName
        {
            get
            {
                object[] assemblyTitleAttributes =
                    assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);

                if (assemblyTitleAttributes.Length > 0)
                {
                    return ((AssemblyTitleAttribute)assemblyTitleAttributes[0]).Title;
                }
                else
                {
                    // default to returning the assembly name if no assembly
                    // title has been specified
                    return assembly.GetName().Name;
                }
            }
        }


        /// <summary>
        /// Gets the assembly version.
        /// </summary>
        public Version Version
        {
            get
            {
                return assembly.GetName().Version;
            }
        }


        /// <summary>
        /// Gets the URI for the application's project home page.
        /// </summary>
        public Uri WebsiteUri
        {
            get
            {
                Uri uri = new Uri(
                    "http://code.google.com/p/watson-bleakness-eliminator/");

                return uri;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Obtain a string representation of the current assembly
        /// (AssemblyInfo).
        /// </summary>
        /// <returns>
        /// System.String
        /// <para>
        /// String including assembly display name and version.
        /// <example>Example: Company.Namespace 1.0.0.0</example>
        /// </para>
        /// </returns>
        public override string ToString()
        {
            return String.Format(CultureInfo.CurrentCulture, "{0} {1}",
                DisplayName, Version);
        }

        #endregion

        #region Public Static Properties

        /// <summary>
        /// Gets the default instance of the AssemblyInfo class.
        /// </summary>
        public static AssemblyInfo Default
        {
            get
            {
                return defaultInstance;
            }
        }

        #endregion
    }
}
